<template>
<div class="main">
    <carousel></carousel>
    <div class="container">
      <!-- 分类 -->
      <div class="row">
        <div class="column">
          <h2 class="category">分类</h2>
          <div class="products">
            <el-menu :default-active="activeIndex"
              class="el-menu-demo"
              mode="horizontal" router>
              <el-submenu :index="item.id + ''" v-for="item in parentCateList" :key="item.id">
                <template slot="title" v-if="item">{{ item.name}}</template>
                <el-menu-item :index="`category/${subCategory.name}`" v-for="subCategory in item.children" :key="subCategory.id + ''">
                  {{ subCategory.name }}</el-menu-item>
              </el-submenu>
            </el-menu>
          </div>
        </div>
      </div>
    </div>
    <Product :items="product" title="宠物" />
    <Product :items="pet" title="宠物商品" />
  </div>
</template>

<script>
import Carousel from '@/components/user/Carousel.vue'
import Product from '@/components/user/Produce.vue'
// import Carousel from '../../components/user/Carousel.vue'
// import Product from '../../components/user/Produce.vue'
export default {
  components: { Carousel, Product },
  data() {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品数据
      product: [],
      // 宠物数据
      pet: [],
      // 分类数据
      parentCateList: [],
      activeIndex: '0'
    }
  },
  created() {
    this.getProduct()
    this.getPet()
    this.getParentCateList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    async getProduct() {
      const { data: res } = await this.$axios.get('/product/cate/' + 16)
      // this.product = res.data.product
      const products = res.data.product
      const selectedProducts = []
      while (selectedProducts.length < 8) {
        const randomIndex = Math.floor(Math.random() * products.length)
        const randomProduct = products[randomIndex]
        if (!selectedProducts.includes(randomProduct)) {
          selectedProducts.push(randomProduct)
        }
      }
      this.product = selectedProducts
    },
    async getPet() {
      const { data: res } = await this.$axios.get('/product/cate/' + 1)
      this.pet = res.data.product
    },
    async getParentCateList() {
      const { data: res } = await this.$axios.get('/product-categories/selectProduct',
      { params: { type: 2, pagenum: 1, pagesize: 10 } })
      if (res.code !== 200) {
       return this.$message.error('获取数据失败！ ')
      }
      this.parentCateList = res.data.prolist
    }
  }
}
</script>

<style lang="less" scoped>
  .row {
    display: flex;
    justify-content: center;
  }

  .column {
    margin: 20px;
    width: 1200px;
  }

  .category {
    text-align: center;
  }

  .products {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .product-item {
    margin: 10px;
    /* Calculate width: subtract the total margin space (left and right)
      from the 1/4th of the parent container. */
    /* You may have to adjust the values if you're providing padding or border. */
    width: calc(20% - 10px);
    border: 1px solid #ccc;
    border-radius: 5px;
    padding: 10px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
  }

  .product-item:hover {
    box-shadow: 0 0 10px rgb(221, 3, 3);
  }

  .product-item a {
    text-decoration: none;
    color: inherit;
  }

  .product-item img {
    width: 100%;
    height: auto;
    border-radius: 5px;
  }

  .details {
    margin-top: 10px;
    text-align: center;
    font-size: 25px;
  }

  .name {
    font-weight: bold;
  }

  .price {
    color: #ff0000;
  }
</style>
